package com.ymatou.iapi.productprice.testcase;

import com.google.common.reflect.TypeToken;
import com.google.gson.JsonObject;
import com.shop2cn.iapi.activitymgt.parameter.AddactivityBean;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsDto;
import com.ymatou.iapi.productmanager.parameter.SaveGrouponBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productprice.parameter.CatalogPrice;
import com.ymatou.iapi.productprice.parameter.deliveryExtra.CatalogDeliveryInfo;
import com.ymatou.iapi.productprice.parameter.deliveryExtra.GetCatalogPriceListByDeliveryExtraRequest;
import com.ymatou.iapi.productprice.service.GetCatalogPriceListByDeliveryExtraCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.*;
import com.ymttest.database.model.GrouponProducts;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.AgentUsers;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Ts_GetCatalogPriceListByDeliveryExtra {
    private static GetCatalogPriceListByDeliveryExtraCall getCatalogPriceListByDeliveryExtraCall ;
    private static GetCatalogPriceListByDeliveryExtraRequest getCatalogPriceListByDeliveryExtraRequest = new GetCatalogPriceListByDeliveryExtraRequest();
    private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
    private YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
    private static int buyerId = ProductPriceCallService.getOldCustomer();
    /**
     * 优选
     */
    private static Integer sellerId = 3383;

    private static int vipRootUserId = AgentUsers.vipRootUserId;
    private static String vipRootUserName = "NickautoTest";
    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("获取多物流下的商品列表规格价");
    	try {
			GodfreightmgrService.deleteBySellerId(rootUserId);
		} catch (IOException e) {
			e.printStackTrace();
		}
    }

    @Before
    public void caseUp() {
        getCatalogPriceListByDeliveryExtraRequest = new GetCatalogPriceListByDeliveryExtraRequest();
        getCatalogPriceListByDeliveryExtraCall = new GetCatalogPriceListByDeliveryExtraCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(getCatalogPriceListByDeliveryExtraCall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    private void setStock(AddSellerProductRequest addpro, Integer catalogNum) {
        addpro.getProduct().getCatalogList().forEach(catalog -> {
            catalog.setStock(catalogNum);
        });
    }

    private void setPrice(AddSellerProductRequest addpro, Double price) {
        addpro.getProduct().getCatalogList().forEach(catalog -> {
            catalog.setNewPrice(price);
            catalog.setMarketPrice(price);
            catalog.setVipPrice(price);
            catalog.setPrice(price);
        });
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetCatalogPriceListByDeliveryExtra_001() {
        Logger.start(true, "获取多物流下的商品列表规格价");
        try {
            AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
            setStock(addpro, 30);
            setPrice(addpro, 100d);
            String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
            List<String> catalogs = ymtCatalogsIWapper.selectCatalogsByProductId(pid).stream().map(x->x.getsCatalogId()).collect(Collectors.toList());
            Double groupPrice = 50d;
            SaveGrouponBean saveGrouponBean = ProductManagerCallServiceV3.initSaveGrouponBean(sellerId,pid,groupPrice);
            ProductManagerCallServiceV3.SaveGrouponCall(saveGrouponBean);
            List<GrouponProducts> gp = ymtProductsIWapper.selectGrouponProducts(pid, null, 0);

            getCatalogPriceListByDeliveryExtraRequest.setBuyerId(buyerId);
            List<CatalogDeliveryInfo> catalogDeliveryInfos = new ArrayList<>();
            catalogs.forEach(p->{
                CatalogDeliveryInfo catalogDeliveryInfo = new CatalogDeliveryInfo();
                catalogDeliveryInfo.setProductId(pid);
                catalogDeliveryInfo.setCatalogId(p);
                catalogDeliveryInfo.setDeliveryType(addpro.getProduct().getCatalogStatus());
                catalogDeliveryInfo.setGrouponVersion(Integer.parseInt(gp.get(0).getVersion().toString()));
                catalogDeliveryInfos.add(catalogDeliveryInfo);
            });

            getCatalogPriceListByDeliveryExtraRequest.setCatalogList(catalogDeliveryInfos);
            Thread.sleep(2000);
            getCatalogPriceListByDeliveryExtraCall.setData(getCatalogPriceListByDeliveryExtraRequest);
            getCatalogPriceListByDeliveryExtraCall.callService();
            Logger.verifyEquals("200",getCatalogPriceListByDeliveryExtraCall.getString("Code"),"验证Code");
            String CatalogPrice = getCatalogPriceListByDeliveryExtraCall.getJsonObject().get("Data").getAsJsonObject().get("CatalogPriceList").getAsJsonArray().toString();
            List<CatalogPrice> CatalogPriceList=(List<CatalogPrice>)FormatUtil.GSONToObject(CatalogPrice, new TypeToken<List<CatalogPrice>>(){}.getType());
            Logger.debug(CatalogPriceList);
            CatalogPriceList.forEach(p->{
                Logger.verifyEquals(pid,p.getProductId(),"验证pid");
                Logger.verifyEquals(groupPrice,p.getCatalogInfo().getPrice(),"验证价格");
                Logger.verifyEquals(4,p.getCatalogInfo().getPriceType(),"验证PriceType");
                Logger.verifyEquals(addpro.getProduct().getCatalogStatus(),p.getDeliveryType(),"验证DeliveryType");
            });
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetCatalogPriceListByDeliveryExtra_002() {
        Logger.start(true, "获取多物流下的商品列表规格价,错误的拼团版本号");
        try {
            AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
            setStock(addpro, 30);
            setPrice(addpro, 100d);
            String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
            List<String> catalogs = ymtCatalogsIWapper.selectCatalogsByProductId(pid).stream().map(x -> x.getsCatalogId()).collect(Collectors.toList());

            getCatalogPriceListByDeliveryExtraRequest.setBuyerId(buyerId);
            List<CatalogDeliveryInfo> catalogDeliveryInfos = new ArrayList<>();
            catalogs.forEach(p -> {
                CatalogDeliveryInfo catalogDeliveryInfo = new CatalogDeliveryInfo();
                catalogDeliveryInfo.setProductId(pid);
                catalogDeliveryInfo.setCatalogId(p);
                catalogDeliveryInfo.setDeliveryType(addpro.getProduct().getCatalogStatus());
                catalogDeliveryInfo.setGrouponVersion(11);
                catalogDeliveryInfos.add(catalogDeliveryInfo);
            });

            getCatalogPriceListByDeliveryExtraRequest.setCatalogList(catalogDeliveryInfos);
            getCatalogPriceListByDeliveryExtraCall.setData(getCatalogPriceListByDeliveryExtraRequest);
            getCatalogPriceListByDeliveryExtraCall.callService();
            Logger.verifyEquals("201", getCatalogPriceListByDeliveryExtraCall.getString("Code"), "商品信息不存在");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }


    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetCatalogPriceListByDeliveryExtra_003() {
        Logger.start(true, "获取多物流下的商品列表规格价，新哈尼");
        try {
            AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
            setStock(addpro, 30);
            setPrice(addpro, 100d);
            addpro.getProduct().getCatalogList().forEach(p->{
                p.setNewPrice(50d);
            });
            String pid = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
            List<String> catalogs = ymtCatalogsIWapper.selectCatalogsByProductId(pid).stream().map(x->x.getsCatalogId()).collect(Collectors.toList());

            UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
            UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
            Integer userId = urrc.getUserid();

            getCatalogPriceListByDeliveryExtraRequest.setBuyerId(userId);
            List<CatalogDeliveryInfo> catalogDeliveryInfos = new ArrayList<>();
            catalogs.forEach(p->{
                CatalogDeliveryInfo catalogDeliveryInfo = new CatalogDeliveryInfo();
                catalogDeliveryInfo.setProductId(pid);
                catalogDeliveryInfo.setCatalogId(p);
                catalogDeliveryInfo.setDeliveryType(addpro.getProduct().getCatalogStatus());
                catalogDeliveryInfos.add(catalogDeliveryInfo);
            });

            Thread.sleep(2000);
            getCatalogPriceListByDeliveryExtraRequest.setCatalogList(catalogDeliveryInfos);
            getCatalogPriceListByDeliveryExtraCall.setData(getCatalogPriceListByDeliveryExtraRequest);
            getCatalogPriceListByDeliveryExtraCall.callService();
            Logger.verifyEquals("200",getCatalogPriceListByDeliveryExtraCall.getString("Code"),"验证Code");
            String CatalogPrice = getCatalogPriceListByDeliveryExtraCall.getJsonObject().get("Data").getAsJsonObject().get("CatalogPriceList").getAsJsonArray().toString();
            List<CatalogPrice> CatalogPriceList=(List<CatalogPrice>)FormatUtil.GSONToObject(CatalogPrice, new TypeToken<List<CatalogPrice>>(){}.getType());
            Logger.debug(CatalogPriceList);
            CatalogPriceList.forEach(p->{
                Logger.verifyEquals(pid,p.getProductId(),"验证pid");
                Logger.verifyEquals(1,p.getCatalogInfo().getPriceType(),"验证PriceType");
                Logger.verifyEquals(addpro.getProduct().getCatalogStatus(),p.getDeliveryType(),"验证DeliveryType");
            });
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetCatalogPriceListByDeliveryExtra_004() {
        Logger.start(true, "神奇商品，NeedCurrencyAndRate为true,验证币种字段");
        try {
            AddAgentProductBean addpro = AddAgentProductCallService.initAddAgentProduct(vipRootUserId, vipRootUserName, false);
            String pid = AddAgentProductCallService.AddAgentProduct(addpro);
            List<String> catalogs = ymtCatalogsIWapper.selectCatalogsByProductId(pid).stream().map(x->x.getsCatalogId()).collect(Collectors.toList());

            UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
            UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
            Integer userId = urrc.getUserid();

            getCatalogPriceListByDeliveryExtraRequest.setBuyerId(userId);
            getCatalogPriceListByDeliveryExtraRequest.setNeedCurrencyAndRate(true);
            List<CatalogDeliveryInfo> catalogDeliveryInfos = new ArrayList<>();
            catalogs.forEach(p->{
                CatalogDeliveryInfo catalogDeliveryInfo = new CatalogDeliveryInfo();
                catalogDeliveryInfo.setProductId(pid);
                catalogDeliveryInfo.setCatalogId(p);
                catalogDeliveryInfo.setDeliveryType(addpro.getCatalogStatus());
                catalogDeliveryInfos.add(catalogDeliveryInfo);
            });

            Thread.sleep(2000);
            getCatalogPriceListByDeliveryExtraRequest.setCatalogList(catalogDeliveryInfos);
            getCatalogPriceListByDeliveryExtraCall.setData(getCatalogPriceListByDeliveryExtraRequest);
            getCatalogPriceListByDeliveryExtraCall.callService();
            Logger.verifyEquals("200",getCatalogPriceListByDeliveryExtraCall.getString("Code"),"验证Code");
            String CatalogPrice = getCatalogPriceListByDeliveryExtraCall.getJsonObject().get("Data").getAsJsonObject().get("CatalogPriceList").getAsJsonArray().toString();
            List<CatalogPrice> CatalogPriceList=(List<CatalogPrice>)FormatUtil.GSONToObject(CatalogPrice, new TypeToken<List<CatalogPrice>>(){}.getType());
            Logger.debug(CatalogPriceList);
            CatalogPriceList.forEach(p->{
                Logger.verifyEquals(pid,p.getProductId(),"验证pid");
                Logger.verifyEquals(0,p.getCatalogInfo().getPriceType(),"验证PriceType");
                Logger.verifyEquals(addpro.getCatalogStatus(),p.getDeliveryType(),"验证DeliveryType");
                Logger.verifyEquals(addpro.getCurrency(),p.getCurrencyExchangeRate().getCurrency(),"验证币种");
            });
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetCatalogPriceListByDeliveryExtra_005() {
        Logger.start(true, "神奇商品，NeedCurrencyAndRate为false,验证币种字段为空");
        try {
            AddAgentProductBean addpro = AddAgentProductCallService.initAddAgentProduct(vipRootUserId, vipRootUserName, false);
            String pid = AddAgentProductCallService.AddAgentProduct(addpro);
            List<String> catalogs = ymtCatalogsIWapper.selectCatalogsByProductId(pid).stream().map(x->x.getsCatalogId()).collect(Collectors.toList());

            UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
            UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
            Integer userId = urrc.getUserid();

            getCatalogPriceListByDeliveryExtraRequest.setBuyerId(userId);
            getCatalogPriceListByDeliveryExtraRequest.setNeedCurrencyAndRate(false);
            List<CatalogDeliveryInfo> catalogDeliveryInfos = new ArrayList<>();
            catalogs.forEach(p->{
                CatalogDeliveryInfo catalogDeliveryInfo = new CatalogDeliveryInfo();
                catalogDeliveryInfo.setProductId(pid);
                catalogDeliveryInfo.setCatalogId(p);
                catalogDeliveryInfo.setDeliveryType(addpro.getCatalogStatus());
                catalogDeliveryInfos.add(catalogDeliveryInfo);
            });

            Thread.sleep(2000);
            getCatalogPriceListByDeliveryExtraRequest.setCatalogList(catalogDeliveryInfos);
            getCatalogPriceListByDeliveryExtraCall.setData(getCatalogPriceListByDeliveryExtraRequest);
            getCatalogPriceListByDeliveryExtraCall.callService();
            Logger.verifyEquals("200",getCatalogPriceListByDeliveryExtraCall.getString("Code"),"验证Code");
            String CatalogPrice = getCatalogPriceListByDeliveryExtraCall.getJsonObject().get("Data").getAsJsonObject().get("CatalogPriceList").getAsJsonArray().toString();
            List<CatalogPrice> CatalogPriceList=(List<CatalogPrice>)FormatUtil.GSONToObject(CatalogPrice, new TypeToken<List<CatalogPrice>>(){}.getType());
            Logger.debug(CatalogPriceList);
            CatalogPriceList.forEach(p->{
                Logger.verifyEquals(pid,p.getProductId(),"验证pid");
                Logger.verifyEquals(0,p.getCatalogInfo().getPriceType(),"验证PriceType");
                Logger.verifyEquals(addpro.getCatalogStatus(),p.getDeliveryType(),"验证DeliveryType");
                Logger.verifyEquals(null,p.getCurrencyExchangeRate(),"验证币种");
            });
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    ////////////////////// 忽略case
	// 渠道商
	private static int rootUserId = AgentUsers.rootUserId;
	private static String rootUserName = "NickautoTest";

	@Test
	@Category(P1.class)
	@TestCase
    @Ignore
	public void Tc_GetCatalogPriceListByDeliveryExtra_006() {
		Logger.start(true, "查询限时抢八折后价格");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, false);
			double price = 123.6;
			addAgentProductBean.getCatalogList().forEach(a -> {
				a.getAgentPriceList().forEach(b -> {
					b.setAgentPrice(price);
				});
				a.setDirectPrice(price);
			});
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			// 创建限时抢购活动
			List<String> catalogList = new ArrayList<String>();
			ProductDetailsDto selectProductDetails = AddAgentProductCallService.selectProductDetails(rootUserId,
					productId);
			selectProductDetails.getCatalogList().forEach(a -> {
				catalogList.add(a.getCatalogId());
			});
			AddactivityBean initProductaddactivity = AgentProductCallService.initProductaddactivity(rootUserId,
					productId, catalogList);
			initProductaddactivity.getProductList().forEach(a -> {
				a.getCatalogList().forEach(b -> {
					b.setDiscount(new BigDecimal("8"));
				});
			});
			int activityId = AgentProductCallService.agentProductaddactivity(initProductaddactivity);
			Thread.sleep(4000);
			
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
            UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
            Integer userId = urrc.getUserid();
			getCatalogPriceListByDeliveryExtraRequest.setBuyerId(userId);
			 List<CatalogDeliveryInfo> catalogDeliveryInfos = new ArrayList<>();
			 catalogList.forEach(p->{
	                CatalogDeliveryInfo catalogDeliveryInfo = new CatalogDeliveryInfo();
	                catalogDeliveryInfo.setProductId(productId);
	                catalogDeliveryInfo.setCatalogId(p);
	                catalogDeliveryInfo.setDeliveryType(addAgentProductBean.getCatalogStatus());
	                catalogDeliveryInfos.add(catalogDeliveryInfo);
	            });
	            
			getCatalogPriceListByDeliveryExtraRequest.setCatalogList(catalogDeliveryInfos);
			getCatalogPriceListByDeliveryExtraCall.setData(getCatalogPriceListByDeliveryExtraRequest);
            getCatalogPriceListByDeliveryExtraCall.callService();
            JsonObject asJsonObject1 = getCatalogPriceListByDeliveryExtraCall.getJsonObject().getAsJsonObject("Data").getAsJsonArray("CatalogPriceList").get(0).getAsJsonObject().getAsJsonObject("CatalogInfo");
            JsonObject asJsonObject2 = getCatalogPriceListByDeliveryExtraCall.getJsonObject().getAsJsonObject("Data").getAsJsonArray("CatalogPriceList").get(1).getAsJsonObject().getAsJsonObject("CatalogInfo");
			Logger.verifyEquals(price * 0.8, asJsonObject1.get("Price").getAsDouble(), "验证是否为八折价格");
			Logger.verifyEquals(price * 0.8, asJsonObject2.get("Price").getAsDouble(), "验证是否为八折价格");
			Logger.verifyEquals(price * 0.8, asJsonObject1.get("ActivityPrice").getAsDouble(), "验证是否为八折价格");
			Logger.verifyEquals(price * 0.8, asJsonObject2.get("ActivityPrice").getAsDouble(), "验证是否为八折价格");
			Logger.verifyEquals(price, asJsonObject1.get("QuotePrice").getAsDouble(), "验证是否为原价");
			Logger.verifyEquals(price, asJsonObject2.get("QuotePrice").getAsDouble(), "验证是否为原价");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
